package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Employee;
import cn.zyy.hotel.entity.Menu;
import cn.zyy.hotel.service.MenuService;
import cn.zyy.hotel.service.NoticeService;
import cn.zyy.hotel.utils.*;
import cn.zyy.hotel.vo.MenuVO;
import cn.zyy.hotel.vo.NoticeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private NoticeService noticeService;

    @Autowired
    private RedisTemplate redisTemplate;

    //加载首页左侧菜单导航
    @RequestMapping("/loadMenuList")
    public Object loadMenuList(HttpSession session){
        //创建Map集合，保存MenuInfo菜单信息
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        //创建Map集合，保存homeInfo菜单信息
        Map<String, Object> homeInfo = new LinkedHashMap<String, Object>();
        //创建Map集合，保存logoInfo菜单信息
        Map<String, Object> logoInfo = new LinkedHashMap<String, Object>();
        //查询所有菜单列表
        //List<Menu> menuList = menuService.selectAllMenu();
        //获取当前登录的员工
        Employee employee = (Employee)session.getAttribute(SystemConstant.LOGINUSER);
        //查询当前员工角色的动态菜单列表
        List<Menu> menuList = menuService.selectMenuListByEmployeeId(employee.getId());
        //创建集合，保存菜单关系
        List<MenuNode> menuNodeList = new ArrayList<MenuNode>();
        //循环遍历菜单列表，目的是创建菜单之间的层级关系
        for (Menu m : menuList) {
            if (m.getButt() == 0){
                //创建菜单节点对象
                MenuNode menuNode = new MenuNode();
                menuNode.setHref(session.getServletContext().getContextPath()+m.getHref()); //链接地址
                menuNode.setIcon(m.getIcon()); //菜单图标
                menuNode.setId(m.getId()); //菜单编号
                menuNode.setPid(m.getPid()); //父级菜单编号
                menuNode.setSpread(m.getSpread()); //是否展开
                menuNode.setTarget(m.getTarget()); //打开方式
                menuNode.setTitle(m.getTitle()); //菜单名称
                //将对象添加到集合
                menuNodeList.add(menuNode);
            }
        }
        //保存HomeInfo信息
        homeInfo.put("title","首页");
        homeInfo.put("href",session.getServletContext().getContextPath()+"/admin/desktop");
        //保存logoInfo信息
        logoInfo.put("title","酒店管理系统"); //logo标题
        logoInfo.put("image",session.getServletContext().getContextPath()+"/layui/images/logo.png"); //logo图片
        logoInfo.put("href",session.getServletContext().getContextPath()+"/admin/home.html"); //首页地址
        //将菜单信息添加到MenuInfo集合中
        map.put("menuInfo", TreeUtil.toTree(menuNodeList,0));
        map.put("homeInfo",homeInfo);
        map.put("logoInfo",logoInfo);
        //将公告放到session中
        List<NoticeVO> noticeList;
        if (redisTemplate.hasKey("noticeList")){
            noticeList = redisTemplate.opsForList().range("noticeList", 0, -1);
        }else {
            noticeList = noticeService.selectNoticeList(null);
            for (NoticeVO noticeVO : noticeList) {
                noticeVO.setCreateDateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(noticeVO.getCreateDate()));
            }
            redisTemplate.opsForList().rightPushAll("noticeList",noticeList);
        }
        session.setAttribute("noticeList",noticeList);
        return map;
    }

    //加载菜单管理页面的左侧导航树
    @RequestMapping("/loadMenuTree")
    public Object loadMenuTree(){
        //调用查询所有菜单列表的方法
        List<Menu> menuList = menuService.selectAllMenu();
        //创建集合保存节点信息
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        //循环遍历菜单列表集合
        for (Menu menu : menuList) {
            //判断当前菜单是否展开
            Boolean spread = (menu.getSpread() == null || menu.getSpread() == 1) ? true : false;
            //将菜单信息保存到treeNodes集合中
            treeNodes.add(TreeNode.builder().id(menu.getId()).parentId(menu.getPid()).title(menu.getTitle()).spread(spread).checkArr("0").butt(menu.getButt()).permissions(menu.getPermissions()).build());
        }
        //返回数据
        return Result.builder().code(0).data(treeNodes).build();
    }

    //分页查询菜单列表
    @RequestMapping("/list")
    public Result list(MenuVO menuVo){
        //设置分页信息
        PageHelper.startPage(menuVo.getPage(),menuVo.getLimit());
        //调用查询菜单列表的方法
        List<Menu> menuList = menuService.selectMenuListByPage(menuVo);
        //创建分页对象
        PageInfo<Menu> pageInfo = new PageInfo<Menu>(menuList);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加菜单
    @RequiresPermissions("menu:add")
    @RequestMapping("/addMenu")
    public Object addMenu(Menu menu){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用新增菜单的方法
        if (menuService.addMenu(menu)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return map;
    }

    //修改菜单
    @RequiresPermissions("menu:edit")
    @RequestMapping("/updateMenu")
    public Object updateMenu(Menu menu){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用修改菜单的方法
        if (menuService.updateMenu(menu)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return map;
    }

    //查询该菜单下是否有子菜单或者是否有用户在使用
    @RequestMapping("/checkMenuHasChild")
    public Object checkMenuHasChild(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用查询是否有子菜单的方法
        if (menuService.getMenuCountByMenuId(id)>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该菜单下有子菜单，无法删除");
            return map;
        }
        //调用查询菜单是否有用户使用
        if (menuService.getRoleCountByMenuId(id)>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该菜单有用户在使用，无法删除");
            return map;
        }
        map.put(SystemConstant.EXIST,false);
        return map;
    }

    //删除菜单
    @RequiresPermissions("menu:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除菜单的方法
        if (menuService.deleteById(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }



}
